package com.amizah.tooling.databases.jdbc.teradata;

import java.sql.*;

import com.amizah.tooling.databases.jdbc.teradata.TeradataParams;

/**
 *@author Zaheer Abzal
 *@version 1.0.0 
 *<p><b>Description:</b> Handles connection to a Teradata database via JDBC.</p> 
 *<p><b>External Dependents:</b> Teradata JDBC driver which are packaged as JAR files. JDBC drivers 
 *can be obtained from Teradata website ({@link http://www.teradata.com/DownloadCenter/Forum149-1.aspx}).</p>
 *<p><b>Date Created:</b> 2/21/2011 </p>
 *<p><b>Last Updated:</b> 2/21/2011 </p>
 */


public class TeradataHandler {
	
	private Connection dbConnection;
	private TeradataParams params;
	private String driver; 
	
	/**
	 * @param xParams
	 * @param xDriver Pass package string to class from driver JAR as a string in format e.g. "com.teradata.jdbc.TeraDriver".
	 */	
	public TeradataHandler(TeradataParams xParams,String xDriver)
	{
		params = xParams;
		driver = xDriver;
	}
	
		
	public void OpenConnection() throws ClassNotFoundException, SQLException
	{
	            // Load the Teradata Driver
	            Class.forName (driver);

	            // Connect to the Teradata database specified in the URL
	            // and submit userid and password.
	            dbConnection = DriverManager.getConnection (params.getURL(), params.username, params.password);
	            	
	}
	
	public void CloseConnection() throws SQLException
	{
		dbConnection.close();
	}
	
	public String getDriver()
	{
		return driver;
	}
	
	public void setDriver(String xDriver)
	{
		driver = xDriver;
	}
	
	/**
	 * @param query
	 * @return results from query typed as ResultSet (java.sql.ResultSet).
	 * @throws SQLException
	 */
	
	public ResultSet executeSQL(String query) throws SQLException
	{
		PreparedStatement pstatement = dbConnection.prepareStatement(query);
		
		ResultSet result = pstatement.executeQuery();
		  
		return result;
	}
	
}
